package com.project.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.entity.po.Comment;
import com.project.entity.po.OrderCommentRelation;
import com.project.entity.qo.CommentQo;
import com.project.exception.ServiceException;
import com.project.mapper.CommentMapper;
import com.project.mapper.OrderCommentRelationMapper;
import com.project.result.ResponseResult;
import com.project.result.ResultCode;
import com.project.service.CommentService;
import com.project.utils.ShiroUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.Date;
import java.util.List;

/**
 * @ProjectName:
 * @Package:
 * @ClassName:
 * @Author: ZhaoLongShi
 * @CreateDate:
 */
@SuppressWarnings("all")
@Service
public class CommentServiceImpl implements CommentService {
    public static final String DEFAULT_COMMENT_USERNAME = "匿名用户";
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private OrderCommentRelationMapper orderCommentRelationMapper;
    @Resource
    private ValueOperations<String, Object> valueOperations;

    /**
     * 评论
     *
     * @param commentQo
     * @return
     */
    @Override
    public ResponseResult<ResultCode> comment(CommentQo commentQo) {
        commentQo.setCreateTime(new Date());
        if (commentQo.getUsername().isEmpty()) {
            commentQo.setUsername(DEFAULT_COMMENT_USERNAME);
        }
        Long orderId = (Long) ShiroUtils.getSessionAttribute("orderId");
        if (ObjectUtils.isEmpty(orderId)) {
            throw new ServiceException(ResultCode.NO_PARAM);
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentQo, comment);
        int count = commentMapper.comment(comment);
        if (count != 1) {
            throw new ServiceException(ResultCode.ERROR);
        }
        OrderCommentRelation orderCommentRelation = new OrderCommentRelation();
        orderCommentRelation.setCommentId(comment.getCommentId());
        orderCommentRelation.setOrderId(orderId);
        orderCommentRelationMapper.insertOrder(orderCommentRelation);
        return ResponseResult.success(ResultCode.SUCCESS);
    }

    /**
     * 查询评论（分页）
     *
     * @param current
     * @return
     */
    @Override
    public ResponseResult selectComment(int current,int size) {
        Object o = valueOperations.get("comment:page:" + current+":size:"+size);
        if (ObjectUtils.isEmpty(o)) {
            Page<Comment> page = commentMapper.selectComment(current,size);
            if (ObjectUtils.isEmpty(page)) {
                throw new ServiceException(ResultCode.PERMISSION_NO_ACCESS);
            }
            valueOperations.set("comment:page:"+current+":size:"+size, page,
                    Duration.ofMinutes(10));
            return ResponseResult.success(ResultCode.SUCCESS, page);
        }
        return ResponseResult.success(ResultCode.SUCCESS, o);

    }

    /**
     * 删除评论（批量）
     *
     * @param idList
     * @return
     */
    @Override
    public ResponseResult<ResultCode> deleteCommentByIds(List<Long> idList) {
        if (idList == null) {
            throw new ServiceException(ResultCode.PARAMS_NOT_IS_BLANK);
        }
        int count = commentMapper.deleteCommentByIds(idList);
        if (count == 0) {
            throw new ServiceException(ResultCode.PERMISSION_NO_ACCESS);
        }
        return ResponseResult.success(ResultCode.SUCCESS);
    }

    /**
     * 添加评论
     *
     * @param commentQo
     * @return
     */
    @Override
    public ResponseResult<ResultCode> addComment(CommentQo commentQo) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentQo, comment);
        int count = commentMapper.insert(comment);
        if (count != 1) {
            throw new ServiceException(ResultCode.INSERT_FAILED);
        }
        return ResponseResult.success(ResultCode.SUCCESS);
    }

    /**
     * 更新评论
     *
     * @param commentQo
     * @return
     */
    @Override
    public ResponseResult<ResultCode> updateCommentById(CommentQo commentQo) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentQo, comment);
        int count = commentMapper.updateById(comment);
        if (count != 1) {
            throw new ServiceException(ResultCode.PERMISSION_NO_ACCESS);
        }
        return ResponseResult.success(ResultCode.SUCCESS);
    }
}
